Counting Semaphore

There are the scenarios in which more than one processes need to execute in critical section simultaneously. However, counting semaphore can be used when we need to have more than one process in the critical section at the same time. The programming code of semaphore implementation is shown below which includes the structure of semaphore and the logic using which the entry and the exit can be performed in the critical section.

Warning

If you input the value of semaphore less than or equal to 0 than its deadlock and hence will show the warning.

Example to input the value

Here it is shown how the value of semaphore will be submitted.

Message of Acceptance

If you submit the value of semaphore greater than zero than it will accept it and will start the visualization.

Buttons overview

Home : In navbar home button is to jump to main page
Dropdown : To jump to any algorithm
Info panel : Displays the value of each required queue and semaphore.
Add Process: On clicking this button new process will be added.
P0 : On clicking this button P0 process will move forward according to algorithm.
save PDF : To save all the status of each stage of algorithm .

Tool tips

Tooltips will help to get information about the hovered element.
here element with tooltips are:
semaphore, suspended queue, critical section, completed queue, Added, Entry, CS, Exit

To CS

By clicking the button the process will be moved to Critical Section if the Critical section is not occupied or semaphore is greater than zero(0).

Add Processes

By clicking Add Processes new processes will be added with the button and process icon.

To Entry Section

Processes will move to suspended queue if the critical section is occupied.
Example: p2 will be moved to critical section and p3 to suspended queue as semaphore value become less than zero.

Warning

If the process is there in critical section still if any process tries to interrupt then it will show the warning.

Completetion pop-up

Will pop-up if any process is completed.

Info Panel

Here live value changes (in queue or variable) will be shown which can learned at each and every command user gives to button.

Full completion

It's gratitude message to display that all the processes are completed.

Save PDF

By clicking the save PDF the pdf will be formed by collecting all the text in textArea.

Analysis of Counting Semaphore approach

1.  In this mechanism, the entry and exit in the critical section are performed on the basis of the value of counting semaphore. The value of counting semaphore at any point of time indicates the maximum number of processes that can enter in the critical section at the same time.
2.  A process which wants to enter in the critical section first decrease the semaphore value by 1 and then check whether it gets negative or not. If it gets negative then the process is pushed in the list of blocked processes (i.e. q) otherwise it gets enter in the critical section.
3.  When a process exits from the critical section, it increases the counting semaphore by 1 and then checks whether it is negative or zero. If it is negative then that means that at least one process is waiting in the blocked state hence, to ensure bounded waiting, the first process among the list of blocked processes will wake up and gets enter in the critical section.
4.  The processes in the blocked list will get waked in the order in which they slept. If the value of counting semaphore is negative then it states the number of processes in the blocked state while if it is positive then it states the number of slots available in the critical section.
5.  To implement mutual exclusion, the value of counting semaphore is initialized with 1.
6.  It ensures that only one process can be present in the critical section at any given time.

abc